package com.zack.algorithm;

/**
 * 快排 email: miao.du@shuruitech.com author: zack time(default is Create Time may
 * change)：Apr 6, 2017 9:19:35 AM
 **/
public class TestAlg {

    public static void main(String[] args) {
        new TestAlg().testDo();
    }

    public void testDo() {
        double[] arr = new double[] { 1.0, 6.7, 6.0, 0.3, 2.5, 6.0 };
        testG(arr, 0, arr.length - 1);
        System.out.println("[RESULT: ]---" + arr);
        for (double d : arr) {
            System.out.print(d + " < ");
        }
    }

    public void testG(double arr[], int left, int right) {
        if (null == arr || arr.length < 1) {
            return;
        }
        int leftNew = left;
        int rightNew = right;
        if (left < right) {
            double base = arr[left];
            while (true) {
                while (leftNew < rightNew && arr[leftNew] < base) {
                    leftNew++;
                }
                while (rightNew > leftNew && arr[rightNew] > base) {
                    rightNew--;
                }
                if (leftNew < rightNew) {
                    if (arr[leftNew] == arr[rightNew]) {
                        leftNew++;
                    } else {
                        double temp = arr[leftNew];
                        arr[leftNew] = arr[rightNew];
                        arr[rightNew] = temp;
                    }
                } else {
                    break;
                }
            }
            testG(arr, left, leftNew - 1);
            testG(arr, leftNew + 1, right);
        }
    }
}
